import 'learning_item.dart';

class CategoryService {
  /// 获取所有年级列表
  static List<int> getAllGrades() {
    return List.generate(12, (index) => index + 1);
  }

  /// 获取所有学科列表
  static List<String> getAllSubjects(List<LearningItem> items) {
    final subjects = <String>{};
    for (var item in items) {
      if (item.subject.isNotEmpty) {
        subjects.add(item.subject);
      }
    }
    return subjects.toList()..sort();
  }

  /// 获取所有模块列表
  static List<String> getAllModules(List<LearningItem> items) {
    final modules = <String>{};
    for (var item in items) {
      if (item.module.isNotEmpty) {
        modules.add(item.module);
      }
    }
    return modules.toList()..sort();
  }

  /// 根据年级筛选
  static List<LearningItem> filterByGrade(List<LearningItem> items, int grade) {
    return items.where((item) => item.grade == grade).toList();
  }

  /// 根据学科筛选
  static List<LearningItem> filterBySubject(List<LearningItem> items, String subject) {
    return items.where((item) => item.subject == subject).toList();
  }

  /// 根据模块筛选
  static List<LearningItem> filterByModule(List<LearningItem> items, String module) {
    return items.where((item) => item.module == module).toList();
  }

  /// 组合筛选
  static List<LearningItem> filterItems(
    List<LearningItem> items, {
    int? grade,
    String? subject,
    String? module,
  }) {
    var filteredItems = items;
    
    if (grade != null) {
      filteredItems = filterByGrade(filteredItems, grade);
    }
    
    if (subject != null && subject.isNotEmpty) {
      filteredItems = filterBySubject(filteredItems, subject);
    }
    
    if (module != null && module.isNotEmpty) {
      filteredItems = filterByModule(filteredItems, module);
    }
    
    return filteredItems;
  }
}